package mn.idax.exchange.otc.service.impl;

import com.google.common.collect.Maps;
import mn.idax.exchange.otc.domain.ValidateQuota;
import mn.idax.exchange.otc.entity.*;
import mn.idax.exchange.otc.mapper.OTCSysDictionaryItemMapper;
import mn.idax.exchange.otc.mapper.OTCSysDictionaryMapper;
import mn.idax.exchange.otc.service.AccountService;
import mn.idax.exchange.otc.service.CoinService;
import mn.idax.exchange.otc.service.OTCSysDictionaryItemService;
import mn.idax.exchange.otc.util.LogUtils;
import mn.idax.exchange.otc.vo.PayTypeDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import static java.util.Optional.ofNullable;

/**
 * @Auther: shihaiming
 * @Date: 2018/08/06 11:48
 * @Description:
 */
@Service
@SuppressWarnings("ALL")
public class OTCSysDictionaryItemServiceImpl implements OTCSysDictionaryItemService {

    @Autowired
    private OTCSysDictionaryItemMapper otcSysDictionaryItemMapper;

    @Autowired
    private OTCSysDictionaryMapper otcSysDictionaryMapper;

    @Autowired
    private AccountService accountService;

    @Autowired
    private CoinService coinService;

	@Override
	public List<OTCSysDictionaryItem> listItem(String dictCode) {
		return otcSysDictionaryItemMapper.listItem(dictCode);
	}

    @Override
    public List<OTCSysDictionaryItem> listItem(String dictCode ,String lang) {
	    if (StringUtils.isEmpty(lang)){
	        lang = "zh";
        }else{
	        lang = lang.split("_")[0].split("-")[0];
        }
        OTCSysDictionaryExample dictExample = new OTCSysDictionaryExample();
        dictExample.createCriteria()
                .andDictcodeEqualTo(dictCode);
        List<OTCSysDictionary> listDict = this.otcSysDictionaryMapper.selectByExample(dictExample);

        if (listDict.isEmpty()){
            return null;
        }
	    OTCSysDictionaryItemExample otcSysDictionaryItemExample = new OTCSysDictionaryItemExample();
	    otcSysDictionaryItemExample.createCriteria()
                .andDictidEqualTo(listDict.get(0).getId())
                .andLanguageEqualTo(lang.toLowerCase());
        otcSysDictionaryItemExample.setOrderByClause("Sort ASC");
        return otcSysDictionaryItemMapper.selectByExample(otcSysDictionaryItemExample);
    }

	@Override
	public OTCSysDictionaryItem getItemByDesc(String dictCode , String itemDesc) {
        OTCSysDictionaryExample dictExample = new OTCSysDictionaryExample();
        dictExample.createCriteria()
                .andDictcodeEqualTo(dictCode);

        List<OTCSysDictionary> listDict = this.otcSysDictionaryMapper.selectByExample(dictExample);

        if (!listDict.isEmpty()){
            OTCSysDictionaryItemExample dictItemExample = new OTCSysDictionaryItemExample();
            dictItemExample.createCriteria()
                    .andDictidEqualTo(listDict.get(0).getId())
                    .andItemdescEqualTo(itemDesc);

            List<OTCSysDictionaryItem> listItem = this.otcSysDictionaryItemMapper.selectByExample(dictItemExample);

            return ofNullable(listItem).map(item -> item.get(0)).orElse(null);

        }
		return null;
	}

    @Override
    public OTCSysDictionaryItem getItemByCode(String dictCode, String itemCode) {
        OTCSysDictionaryExample dictExample = new OTCSysDictionaryExample();
        dictExample.createCriteria()
                .andDictcodeEqualTo(dictCode);

        List<OTCSysDictionary> listDict = this.otcSysDictionaryMapper.selectByExample(dictExample);

        if (!listDict.isEmpty()){
            OTCSysDictionaryItemExample dictItemExample = new OTCSysDictionaryItemExample();
            dictItemExample.createCriteria()
                    .andDictidEqualTo(listDict.get(0).getId())
                    .andTextEqualTo(itemCode);

            List<OTCSysDictionaryItem> listItem = this.otcSysDictionaryItemMapper.selectByExample(dictItemExample);

            return ofNullable(listItem).map(item -> item.get(0)).orElse(null);

        }
        return null;
    }

    @Override
    public OTCSysDictionaryItem getItemByDictIdAndItemValue(Integer dictId, Integer itemValue) {

        OTCSysDictionaryItemExample example = new OTCSysDictionaryItemExample();
        example.createCriteria().andDictidEqualTo(dictId).andItemvalueEqualTo(itemValue);
        List<OTCSysDictionaryItem> otcSysDictionaryItems = otcSysDictionaryItemMapper.selectByExample(example);
        return ofNullable(otcSysDictionaryItems).map(item -> item.get(0)).orElse(null);
    }

    //出售广告时获取最小限额和最大限额
    @Override
    public Map<String, String> getMinamountAndMaxAmount(ValidateQuota validateQuota,Integer userId) {

	    //用户数字币数量 availableamount 数字币最大限额 digitalCoinQuota 法币最小限额 currencyQuota
        Map<String, String> map = Maps.newHashMap();
        LogUtils.info(this.getClass(), "查询的数字币是 ******* " + validateQuota.getPairname());
        //配合idax 3.0 查账户
        Integer coinId = coinService.getCoinIdByCode(validateQuota.getPairname());
        Account account = accountService.getAccountByCoinIdAndUserId(userId, coinId);
        LogUtils.info(this.getClass(), "");
        //账户可用资产
        if (account != null){
            map.put("availableamount",((account.getAvailableamount())).toPlainString());
        } else {
            map.put("availableamount","0");
        }
        //出售时minAmount
        OTCSysDictionaryItem payUnitDetail = this.getPayUnitDetail(validateQuota.getUnit());
        if (payUnitDetail != null){
            map.put("currencyQuota",payUnitDetail.getText());
        } else {
            map.put("currencyQuota","0");
        }

        //购买时maxAmount
        OTCSysDictionaryItem digitalCoinDetail = this.getDigitalCoinDetail(coinId);
        if (digitalCoinDetail != null){
            map.put("digitalCoinQuota",digitalCoinDetail.getItemdesc());
        } else {
            map.put("digitalCoinQuota","0");
        }


        return map;
    }

    //获取指定法币的详细信息
    @Override
    public OTCSysDictionaryItem getPayUnitDetail(String unit) {
        return otcSysDictionaryItemMapper.getPayUnitDetail(unit);
    }

    //获取指定数字币最大出售额的详细信息
    @Override
    public OTCSysDictionaryItem getDigitalCoinDetail(Integer coinId) {
        return otcSysDictionaryItemMapper.getDigitalCoinDetail(String.valueOf(coinId));
    }

    //场外支持的支付方式
    @Override
    public List<PayTypeDTO> getPayType() {

        return otcSysDictionaryItemMapper.getPayType();
    }
}
